package com.hrsystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.hrsystem.entity.Employee;
import com.hrsystem.entity.OrgThird;
import com.hrsystem.entity.Salary;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.SalaryMapper;
import com.hrsystem.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    SalaryService salaryService;

    @Resource
    SalaryMapper salaryMapper;


    //添加薪资标准
    @Transactional
    @PostMapping("/add")
    public Result add(@RequestBody Salary salary){
        Salary temp;
        temp = new Salary();
        temp.setSalaryId(salary.getSalaryId());
        temp.setCreateTime(LocalDateTime.now());
        temp.setReviewer(null);
        temp.setReviewerTime(null);
        temp.setIsReview(false);
        BeanUtil.copyProperties(salary, temp, "SalaryId", "CreateTime","IsReview");
        salaryService.saveOrUpdate(temp);
        return Result.success(null);
    }

    //查看薪资标准复核简要
    @Transactional
    @GetMapping("/reviewSelectBriefly")
    public Result reviewSelectBriefly(){
        QueryWrapper<Salary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDelete",false);
        queryWrapper.eq("isReview",false);
        queryWrapper.select("Salary_id","salaryName","Maker","cJob_id","nJob_id","tJob_id","Total");
        List<Salary> selectSalaryList = salaryMapper.selectList(queryWrapper);
        return Result.success(selectSalaryList);
    }

    //查看薪资标准变更
    @Transactional
    @GetMapping("/changeSelectBriefly")
    public Result changeSelectBriefly(){
        QueryWrapper<Salary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDelete",false);
        queryWrapper.eq("isReview",true);
        queryWrapper.select("Salary_id","salaryName","Maker","cJob_id","nJob_id","tJob_id","Total");
        List<Salary> selectSalaryList = salaryMapper.selectList(queryWrapper);
        return Result.success(selectSalaryList);
    }

    //查看薪资复核详情
    @Transactional
    @GetMapping("/ReviewDetail/{id}")
    public Result detail(@PathVariable(name = "id") Long id){
        Salary salary = salaryService.getById(id);
        //TODO 要加一个Assert.notNull（salary,"该档案已经删除'）非空断言检测，等Shiro引入后再加
        return Result.success(salary);
    }

    //再薪资复核中修改
    @Transactional
    @PostMapping("/reviewSalary")
    public Result reviewSalary(@RequestBody Salary salary) {
        Salary temp;
        temp = new Salary();
        temp.setReviewerTime(LocalDateTime.now());
        //TODO 复核人等登录写好再说
        temp.setIsReview(true);
        BeanUtil.copyProperties(salary, temp, "reviewerTime", "IsReview");
        salaryService.saveOrUpdate(temp);
        return Result.success(null);
    }

    //在薪资变更中修改
    @Transactional
    @PostMapping("/changeSalary")
    public Result changeFile(@RequestBody Salary salary) {
        Salary temp;
        temp = new Salary();
        temp.setIsReview(false);
        BeanUtil.copyProperties(salary, temp);
        salaryService.saveOrUpdate(temp);
        return Result.success(null);
    }


    //查询薪资标准
    @Transactional
    @GetMapping("/selectSalary/{id}")
    public Result selectSalaryList(@PathVariable(name = "id") Long id){
        QueryWrapper<Salary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tJob_id",id);
        queryWrapper.eq("IsReview",true);
        List<Salary> salaryList = salaryMapper.selectList(queryWrapper);
        return Result.success(salaryList);
    }


    //查询薪资标准（有条件查询）
    @Transactional
    @GetMapping("/queryFile/{salaryId}/{message}/{timeValueStart}/{timeValueEnd}")
    public Result queryFile(@PathVariable(name = "salaryId") String salaryId, @PathVariable(name = "message") String message, @PathVariable(name = "timeValueStart") String timeValueStart, @PathVariable(name = "timeValueEnd") String  timeValueEnd) throws ParseException {
        QueryWrapper<Salary> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (!salaryId.equals("empty")){
            queryWrapper.eq("Salary_id",Long.parseLong(salaryId));
        }
        if (!message.equals("empty")){
            queryWrapper.like("salaryName",message);
        }
        if (!timeValueStart.equals("undefined") && !timeValueEnd.equals("undefined")){
            Date timeValueStartDate = simpleDateFormat.parse(timeValueStart);
            Date timeValueEndDate = simpleDateFormat.parse(timeValueEnd);
            queryWrapper.between("CreateTime", timeValueStartDate ,timeValueEndDate);
        }
        queryWrapper.eq("isReview",true);
        queryWrapper.eq("isDelete",false);
        queryWrapper.select("Salary_id","salaryName","Maker","cJob_id","nJob_id","tJob_id","Total");
        List<Salary> selectSalaryList = salaryMapper.selectList(queryWrapper);
        return Result.success(selectSalaryList);
    }

}
